package com.mge.ae.data;

import java.util.Iterator;
import java.util.Vector;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.eclipse.swt.internal.win32.CREATESTRUCT;

/**
 * 图片切块列表数据类
 * @author wangbin
 *
 */
public class ImageSplitTileData extends ResourceAbstract{
	
	private String srcPath = "";	//依赖图片的路径
	private Vector<ImageSplitTileItemData> istis;
	private int curItemID;	//当前切块的最大id
	
	public ImageSplitTileData(Identity identity){
		setType(TYPE_IMAGE_SPLIT_TILE);
		this.identity = identity;
		istis = new Vector<ImageSplitTileItemData>();
	}
	
	public static void main(String[] args){
		Identity identity = new Identity();
		identity.url = "d:/test.ist";
		ImageSplitTileData imageSplitTileData = new ImageSplitTileData(identity);
//		for(int i = 0; i < 5; i++){
//			ImageSplitTileItemData itemData = imageSplitTileData.creatNewItem();
//			itemData.setId(i);
//			itemData.setOffsetX(i + 2);
//			itemData.setOffsetY(i + 2);
//			itemData.setWidth(i + 5);
//			itemData.setHeight(i + 5);
//		}
//		imageSplitTileData.save();
		imageSplitTileData.read(identity.url);
	}
	
	@Override
	public int save() {
		// TODO Auto-generated method stub
		return export(getIdentity().url, 0);
	}

	@Override
	public int read() {
		return read(getIdentity().url);
	}
	
	@Override
	public int read(String path) {
		// TODO Auto-generated method stub
		int state = STATE_ERROR;
		Document document = readXML4Document(path);
		try {
			Element root = document.getRootElement();
			String type = root.elementText(TYPE);
			setType(Byte.parseByte(type));
			String version = root.elementText(VERSION);
			getIdentity().version = Long.parseLong(version);
			String lastModify = root.elementText(LAST_MODIFY);
			setLastModify(lastModify);
			Element src = root.element(SRC_PATH);
			String srcP = src.elementText(ITEM);
			setSrcPath(srcP);
			
			Element items = root.element(ITEMS);
			Iterator itemsI = items.elementIterator();
			deleteAllItem();
			while(itemsI.hasNext()){
				Element element = (Element)itemsI.next();
				String id = element.elementText(ID);
				ImageSplitTileItemData itemData = new ImageSplitTileItemData(Integer.parseInt(id));
				String x = element.elementText(OFFSET_X);
				itemData.setOffsetX(Integer.parseInt(x));
				String y = element.elementText(OFFSET_Y);
				itemData.setOffsetY(Integer.parseInt(y));
				String w = element.elementText(WIDTH);
				itemData.setWidth(Integer.parseInt(w));
				String h = element.elementText(HEIGHT);
				itemData.setHeight(Integer.parseInt(h));
				addItem(itemData);
			}
			state = STATE_SUCCECSS;
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return state;
	}
	
	@Override
	public int export(String path, int type) {
		// TODO Auto-generated method stub
		beforeSave();
		
		Document document = DocumentHelper.createDocument();
		Element root = document.addElement(MGE_RESOURCE);
		root.addElement(TYPE).setText(String.valueOf(getType()));
		root.addElement(VERSION).setText(String.valueOf(getIdentity().version));
		root.addElement(LAST_MODIFY).setText(getLastModify());
		root.addElement(SRC_PATH)
				.addElement(ITEM).setText(getSrcPath());
		Element items = root.addElement(ITEMS);
		synchronized (istis) {
			for(int i = 0; i < istis.size(); i++){
				ImageSplitTileItemData itemData = istis.elementAt(i);
				Element item = items.addElement(ITEM);
				item.addElement(ID).setText(String.valueOf(itemData.getId()));
				item.addElement(OFFSET_X).setText(String.valueOf(itemData.getOffsetX()));
				item.addElement(OFFSET_Y).setText(String.valueOf(itemData.getOffsetY()));
				item.addElement(WIDTH).setText(String.valueOf(itemData.getWidth()));
				item.addElement(HEIGHT).setText(String.valueOf(itemData.getHeight()));
			}
		}
		int state = saveFormatXML(document, path);
		return state;
	}
	
//	private int 
	
	/**
	 * 创建一个新的切块
	 * @return
	 */
	public ImageSplitTileItemData creatNewItem(){
		ImageSplitTileItemData itemData = new ImageSplitTileItemData(curItemID);
		curItemID ++;
		addItem(itemData);
		return itemData;
	}
	
	/**
	 * 添加一个切块
	 * @param tileItemData
	 */
	public void addItem(ImageSplitTileItemData tileItemData){
		this.istis.addElement(tileItemData);
	}

	/**
	 * 删除指定id的切块
	 * @param id
	 */
	public void deleteItem(int id){
		for(int i = 0; i < istis.size(); i++){
			ImageSplitTileItemData data = istis.elementAt(i);
			if(data.getId() == id){
				istis.removeElementAt(i);
				break;
			}
		}
	}
	
	/**
	 * 删除指定切块
	 * @param itemData
	 */
	public void deleteItem(ImageSplitTileItemData itemData){
		for(int i = 0; i < istis.size(); i++){
			ImageSplitTileItemData data = istis.elementAt(i);
			if(data == itemData){
				istis.removeElementAt(i);
				break;
			}
		}
	}
	
	/**
	 * 删除所有切块
	 */
	public void deleteAllItem(){
		istis.removeAllElements();
	}
	
	/**
	 * 得到指定id的切片
	 * @param id
	 * @return
	 */
	public ImageSplitTileItemData getItem(int id){
		for(int i = 0; i < istis.size(); i++){
			ImageSplitTileItemData data = istis.elementAt(i);
			if(data.getId() == id){
				return data;
			}
		}
		return null;
	}

	public String getSrcPath() {
		return srcPath;
	}

	public void setSrcPath(String srcPath) {
		this.srcPath = srcPath;
	}
}
